package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.item.ItemFeignClient;
import com.atguigu.gmall.feign.product.ProductFeignClient;
import com.atguigu.gmall.model.to.SkuDetailTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.math.BigDecimal;

@Slf4j
@Controller
public class ItemController {

    @Autowired
    ItemFeignClient itemFeignClient;
    @Autowired
    ProductFeignClient productFeignClient;

    @GetMapping("/{skuId}.html")
    public String itemPage(@PathVariable("skuId") Long skuId,
                           Model model) {
        log.info("商品详情:{}", skuId);
        //查询数据
        Result<SkuDetailTo> skuItemDetail = itemFeignClient.getSkuItemDetail(skuId);

        if (skuItemDetail.isOk()) {
            SkuDetailTo data = skuItemDetail.getData();
            //1.当前sku所在的分类完整信息s
            model.addAttribute("categoryView", data.getCategoryView());

            //2. 当前sku信息
            model.addAttribute("skuInfo", data.getSkuInfo());





            //4.sku的spu所有销售属性集合
            model.addAttribute("spuSaleAttrList",data.getSpuSaleAttrList());
            //sku下的所有可用销售属性组合
            model.addAttribute("valuesSkuJson", data.getValuesSkuJson());
        }
        //3.sku的价格
        Result<BigDecimal> skuPrice = productFeignClient.getSkuPrice(skuId);
        if(skuPrice.isOk()){
            //sku实时价格
            model.addAttribute("price",skuPrice.getData());
        }


        return "item/index";

    }
}
